
import UIKit
import AVKit
import Photos
import KSPhotoBrowser

class PhotoUtil: NSObject {
    
    /// 浏览网络图片
    /// - Parameters:
    ///   - imageUrl: url数组
    ///   - currentIndex: 当前页码
    ///   - sourceView: 原控件
    open class func browserPhoto(_ imageUrl: [URL] = [], currentIndex: Int = 0, sourceView: UIImageView? = nil, _ fromController: UIViewController?) {
        guard !imageUrl.isEmpty else { return }
        var items = [KSPhotoItem]()
        for item in imageUrl {
            let item = KSPhotoItem(sourceView: sourceView, imageUrl: item)
            items.append(item)
        }
        let browser = KSPhotoBrowser(photoItems: items, selectedIndex: UInt(currentIndex))
        browser.pageindicatorStyle = .text
        browser.dismissalStyle = .scale
        browser.modalTransitionStyle = .crossDissolve
        if let currentController = fromController {
            currentController.present(browser, animated: true, completion: {})
        }else {
            if let controller = UIApplication.window?.rootViewController {
                if controller.self.isMember(of: UIViewController.self) {
                    controller.present(browser, animated: true, completion: {})
                }
            }
        }
    }
    
    /// 浏览本地图片
    /// - Parameters:
    ///   - images: 图片数组
    ///   - currentIndex: 当前页码
    ///   - sourceView: 原控件
    open class func browserPhoto(_ images: [UIImage] = [], currentIndex: Int = 0, sourceView: UIImageView? = nil, _ fromController: UIViewController?) {
        guard !images.isEmpty else { return }
        var items = [KSPhotoItem]()
        for item in images {
            let item = KSPhotoItem(sourceView: sourceView, image: item)
            items.append(item)
        }
        let browser = KSPhotoBrowser(photoItems: items, selectedIndex: UInt(currentIndex))
        browser.pageindicatorStyle = .text
        browser.dismissalStyle = .scale
        browser.modalTransitionStyle = .crossDissolve
        if let currentController = fromController {
            currentController.present(browser, animated: true, completion: {})
        }else {
            if let controller = UIApplication.window?.rootViewController {
                if controller.self.isMember(of: UIViewController.self) {
                    controller.present(browser, animated: true, completion: {})
                }
            }
        }
    }

    static let isSimulator: Bool = {
        var isSim = false
        #if arch(i386) || arch(x86_64)
            isSim = true
        #endif
        return isSim
    }()

}
